package com.cet.qz.unit01.chapter1_2;

import edu.princeton.cs.algs4.Interval1D;
import edu.princeton.cs.algs4.Interval2D;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdRandom;

/**
 * @program: algorithm
 * @description:
 * @author: 陈恩涛
 * @create: 2021-02-28 21:51
 **/
public class Qz1_2_3 {

    public static Interval2D[] genNInterval2Ds(int N, double min, double max) {

        Interval2D[] interval2DS = new Interval2D[N];

        for (int i = 0; i < N; i++) {
            double xlo = StdRandom.uniform(min, max);
            double xhi = StdRandom.uniform(min, max);
            Interval1D xinterval1D;
            if (xlo > xhi) {
                xinterval1D = new Interval1D(xhi, xlo);
            } else {
                xinterval1D = new Interval1D(xlo, xhi);
            }

            double ylo = StdRandom.uniform(min, max);
            double yhi = StdRandom.uniform(min, max);
            Interval1D yinterval1D;
            if (ylo > yhi) {
                yinterval1D = new Interval1D(yhi, ylo);
            } else {
                yinterval1D = new Interval1D(ylo, yhi);
            }

            interval2DS[i] = new Interval2D(xinterval1D, yinterval1D);
        }

        return interval2DS;
    }

    public static void drawInterval2Ds(Interval2D[] interval2DS) {
        for (int i = 0; i < interval2DS.length; i++) {
            interval2DS[i].draw();
        }
    }

    public static int countInsectedInterval2Ds(Interval2D[] interval2DS) {
        int length = interval2DS.length;
        int insectedNum = 0;
        for (int i = 0; i < length; i++) {
            for (int j = i+1; j < length; j++) {
                if (interval2DS[i].intersects(interval2DS[j])) {
                    insectedNum++;
                }
            }
        }
        return insectedNum;
    }

    /*public static boolean hasContainsRelation(Interval2D i1, Interval2D i2) {
        if (i1.area() > i2.area()) {
            if (i1) {
            }
        } else {

        }
    }

    public static int countContainsInterval2Ds(Interval2D[] interval2DS) {
        int length = interval2DS.length;
        int containsNum = 0;
        for (int i = 0; i < length; i++) {
            for (int j = i+1; j < length; j++) {
                if () {

                }
            }
        }
        return containsNum;
    }*/

    public static void main(String[] args) {
        System.out.println("输入数要生成的随机二维间隔数：");
        int N = StdIn.readInt();
        System.out.println("输入二维间隔在单位正方形区域的正方形定义域min和max");
        System.out.println("min:");
        double min = StdIn.readDouble();
        System.out.println("max:");
        double max = StdIn.readDouble();
        Interval2D[] interval2DS = genNInterval2Ds(N,min,max);
        drawInterval2Ds(interval2DS);
        System.out.println("相交的二维间隔对数有"+countInsectedInterval2Ds(interval2DS)+"对");
    }
}
